package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlEvent;
import com.ruoyi.sl.service.ISlEventService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 情况汇报Controller
 * 
 * @author admin
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/sl/event")
public class SlEventController extends BaseController
{
    @Autowired
    private ISlEventService slEventService;

    /**
     * 查询情况汇报列表
     */
    @PreAuthorize("@ss.hasPermi('sl:event:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlEvent slEvent)
    {
        startPage();
        //如果不是管理员则只查询登录用户自己的
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()){
            slEvent.setCreateBy(getLoginUser().getUsername());
        }
        List<SlEvent> list = slEventService.selectSlEventList(slEvent);
        return getDataTable(list);
    }

    /**
     * 导出情况汇报列表
     */
    @PreAuthorize("@ss.hasPermi('sl:event:export')")
    @Log(title = "情况汇报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlEvent slEvent)
    {
        //如果不是管理员则只查询登录用户自己的
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()){
            slEvent.setCreateBy(getLoginUser().getUsername());
        }
        List<SlEvent> list = slEventService.selectSlEventList(slEvent);
        ExcelUtil<SlEvent> util = new ExcelUtil<SlEvent>(SlEvent.class);
        util.exportExcel(response, list, "情况汇报数据");
    }

    /**
     * 获取情况汇报详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:event:query')")
    @GetMapping(value = "/{eventId}")
    public AjaxResult getInfo(@PathVariable("eventId") Long eventId)
    {
        return success(slEventService.selectSlEventByEventId(eventId));
    }

    /**
     * 新增情况汇报
     */
    @PreAuthorize("@ss.hasPermi('sl:event:add')")
    @Log(title = "情况汇报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlEvent slEvent)
    {
        slEvent.setCreateBy(getUsername());
        return toAjax(slEventService.insertSlEvent(slEvent));
    }

    /**
     * 修改情况汇报
     */
    @PreAuthorize("@ss.hasPermi('sl:event:edit')")
    @Log(title = "情况汇报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlEvent slEvent)
    {
        slEvent.setUpdateBy(getUsername());
        return toAjax(slEventService.updateSlEvent(slEvent));
    }

    /**
     * 删除情况汇报
     */
    @PreAuthorize("@ss.hasPermi('sl:event:remove')")
    @Log(title = "情况汇报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eventIds}")
    public AjaxResult remove(@PathVariable Long[] eventIds)
    {
        return toAjax(slEventService.deleteSlEventByEventIds(eventIds));
    }
}
